package cn.cvs.test;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.pojo.SysUser;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserTest {

    @Test
    public void testCount() throws IOException {
        // 读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");

        //创建 SqlSessionFactoryBuilder;/.
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //创建 SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //创建 SqlSession
        SqlSession sqlSession = factory.openSession();

        // int count = sqlSession.selectOne("cn.cvs.dao.SysUserMapper.count");

        // 创建SysUserMapper接口 的 实例
        SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class);
        // 调用其方法
        int count = mapper.count();

        System.out.println(count);

        // 关闭 sqlSession
        sqlSession.close();
    }

    @Test
    public void testList() throws IOException {

        //创建 SqlSession
        SqlSession sqlSession = MyBatisUtil.createSqlSession();

        // int count = sqlSession.selectOne("cn.cvs.dao.SysUserMapper.count");

        // 创建SysUserMapper接口 的 实例
        SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class);
        // 调用其方法
        List<SysUser> list = mapper.list();

        list.forEach(sysUser -> {
            System.out.println(sysUser.getId()+"-"+sysUser.getRealName());
        });

        // 关闭 sqlSession
        MyBatisUtil.closeSqlSession(sqlSession);
    }
}
